package Compute;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Win extends JFrame{
    BtnListener listener=new BtnListener();
    public Win(String name,int x,int y,int m,int n) {
        setBounds(x,y,m,n);
        setTitle(name);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        init();
    }
    private void init() {
        JTextField field1=new JTextField(10);
        JTextField field2=new JTextField(10);
        JLabel label1 = new JLabel("数字1");
        JLabel label2 = new JLabel("数字2");
        JTextArea area=new JTextArea(10,30);
        area.setLineWrap(true);
        JButton btn1=new JButton("加");
        JButton btn2=new JButton("减");
        JButton btn3=new JButton("乘");
        JButton btn4=new JButton("处");
        JButton btn5=new JButton("余");
        add(label1);
        add(field1);
        add(btn1);
        add(btn2);
        add(btn3);
        add(btn4);
        add(btn5);
        add(label2);
        add(field2);
        add(area);
        add(new JScrollPane(area));
        listener.setButton(btn1,btn2,btn3,btn4,btn5);
        listener.setField1(field1);
        listener.setField2(field2);
        listener.setArea(area);
        btn1.addActionListener(listener);
        btn2.addActionListener(listener);
        btn3.addActionListener(listener);
        btn4.addActionListener(listener);
        btn5.addActionListener(listener);
    }
}
class BtnListener implements ActionListener{
    JButton btn1,btn2,btn3,btn4,btn5;
    JTextField  field1,field2;
    JTextArea area;
    double result;
    public void setButton(JButton btn1,JButton btn2,JButton btn3,JButton btn4,JButton btn5){
        this.btn1=btn1;
        this.btn2=btn2;
        this.btn3=btn3;
        this.btn4=btn4;
        this.btn5=btn5;
    }
    public void setField1(JTextField field1) {
        this.field1 = field1;
    }
    public void setField2(JTextField field2) {
        this.field2 = field2;
    }
    public void setArea(JTextArea area) {
        this.area = area;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            double getfield1 = Double.parseDouble(field1.getText());
            double getfield2 = Double.parseDouble(field2.getText());
            if (e.getActionCommand().equals("加")) { //两种方式
                result = getfield1 + getfield2;
                area.append(getfield1 + " + " + getfield2 + " = " + result+"\n");
            }
            if (e.getSource()==btn2) {
                result = getfield1 - getfield2;
                area.append(getfield1 + " - " + getfield2 + " = " + result+"\n");
            }
            if (e.getSource()==btn3) {
                result = getfield1 * getfield2;
                area.append(getfield1 + " * " + getfield2 + " = " + result+"\n");
            }
            if (e.getSource()==btn4) {
                result = getfield1 / getfield2;
                area.append(getfield1 + " / " + getfield2 + " = " + result+"\n");
            }
            if (e.getSource()==btn5) {
                result = getfield1 % getfield2;
                area.append(getfield1 + " % " + getfield2 + " = " + result+"\n");
            }
        }catch (Exception exception){
           exception.printStackTrace();
        }
    }
}
public class ComputeExer {
    public static void main(String[] args) {
        Win win = new Win("计算",200,200,400,300);
    }
}
